import React, { Component } from 'react'
import { Card, Form, Input, Button, Checkbox, message } from 'antd'
// import './index.scss'
import style from './index.module.scss'
// 引入图片
import logo from 'assets/logo.png'
import { login } from 'api/user'
import { setStorage } from 'utils/storage'

export default class Login extends Component {
  // onFinish：提交表单且数据验证成功后回调事件
  onFinish = async (values) => {
    // console.log(values)
    try {
      const res = await login(values)
      // console.log(res.data)
      // localStorage.setItem('jikenyuan_token', JSON.stringify(res.data))
      setStorage('jikenyuan_token', res.data)
      // this.props.history.push('/home')
      const { state } = this.props.location
      if (state && state.formpath) {
        this.props.history.push(state.formpath)
      } else {
        this.props.history.push('/home')
      }
      message.success('登录成功')
    } catch (err) {
      message.error('登录失败')
    }
  }

  render() {
    return (
      <div className={style.login}>
        <Card className="login-container">
          <img className="login-logo" src={logo} alt="" />
          {/* onFinish：提交表单且数据验证成功后回调事件 */}
          {/* initialValues:表单默认值，只有初始化以及重置时生效 */}
          <Form
            name="basic"
            size="large"
            validateTrigger={['onBlur', 'onChange']}
            onFinish={this.onFinish}
            initialValues={{
              agree: false,
              mobile: '13888888888',
              code: '246810'
            }}
          >
            <Form.Item
              name="mobile"
              rules={[
                {
                  pattern:
                    /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
                  message: '手机号码格式不对'
                },
                {
                  required: true,
                  message: '请输入手机号'
                }
              ]}
            >
              <Input />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                {
                  pattern: /^\d{6}$/,
                  message: '验证码格式不对'
                },
                { required: true, message: '请输入验证码' }
              ]}
            >
              <Input.Password />
            </Form.Item>

            {/* 需要自定义校验，并要设置子节点的值的属性valuePropName="checked" */}
            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[
                {
                  validator(_, value) {
                    // 判断是否勾选了复选框
                    if (value) return Promise.resolve()
                    return Promise.reject(new Error('请勾选协议'))
                  }
                }
              ]}
            >
              <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button type="primary" htmlType="submit" block>
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }
}
